document.addEventListener('DOMContentLoaded', function() {
    const registerForm = document.getElementById('registerForm');
    
    registerForm.addEventListener('submit', function(e) {
        e.preventDefault();
        
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;
        const confirmPassword = document.getElementById('confirmPassword').value;
        const email = document.getElementById('email').value;
        
        // 验证密码是否匹配
        if (password !== confirmPassword) {
            alert('两次输入的密码不一致！');
            return;
        }
        
        // 验证密码强度
        if (password.length < 6) {
            alert('密码长度至少为6个字符！');
            return;
        }

        // 验证邮箱格式
        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        if (!emailRegex.test(email)) {
            alert('请输入有效的邮箱地址！');
            return;
        }
        
        // 发送注册请求
        fetch('http://localhost:8080/auth/register/teacher', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                username: username,
                password: password,
                email: email
            })
        })
        .then(response => response.json())
        .then(data => {
            if (data.code === 200) {
                alert('注册成功！');
                window.location.href = 'login.html';
            } else {
                alert(data.message || '注册失败！');
            }
        })
        .catch(error => {
            alert('网络错误，请稍后重试！');
            console.error('Error:', error);
        });
    });
}); 